<?php
/********************************************************************
 *	Author:
 *		Jestery Liu <jestery@gmail.com>
 *		http://www.pte.cn
 *
 *	File:			ViewHelper.class.php
 *	Create Date:	2006-12-26 13:45:16 CST
 *******************************************************************/

class ViewHelper
{
	public static function pager($currpage, $perpage, $nums, $q, $currPageStyle='', $othersPageStyle='')
	{
		$dp=10; /* 分页链接的数量 */
		$nums = intval($nums);
		$maxPages = ceil($nums/$perpage);
		$pageStart=1;
		if ($maxPages==0)
		$maxPages = 1;
		if ($currpage>$maxPages)
		$currpage=$maxPages;
		if ($currpage<=1)
		{
			$s = "&lt;&lt; ";
			$pageStart = 1;
			$currpage=1;
			$pageEnd=$dp;
		}
		else
		{
			$tmp = $currpage-1;
			$s = "<a href=\"".str_replace('{page}', $tmp, $q)."\">&lt;&lt;</a> ";
			/*** 下面开始计算 1--$dp 以后的 $pageStart ***/
			$rangeOrder = floor(($currpage-2)/($dp-2));
			$pageStart = $rangeOrder*($dp-2)+1;
			$pageEnd=$pageStart+$dp-1;
		}

		for ($i=$pageStart; $i<=$pageEnd; $i++)
		{
			if ($i>$maxPages)
			break;
			if ($i!=$currpage)
			$s.= '<a href="'.str_replace('{page}', $i, $q).'" class="'.$othersPageStyle.'">'.$i.'</a> ';
			else
			$s.= '<span class="'.$currPageStyle.'">'.$i.'</span> ';
		}

		if ($currpage>=$maxPages)
		{
			$s.= '&gt;&gt;';
		}
		else
		{
			$tmp = $currpage+1;
			$s.= "<a href=\"".str_replace('{page}', $tmp, $q)."\">&gt;&gt;</a>";
		}
		return $s;
	}
}

/* vim: set ts=4 sw=4 ff=unix: */
